﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPWeb.Class;
using KPWeb.Data;
using KPWeb.Function;
using KPWeb.Reports;
using MSExcel = Microsoft.Office.Interop.Excel;
//using Microsoft.Reporting.WebForms;


namespace KPWeb.AnkietaOcenaPracownika
{

    public partial class DokumentOcenaPracownika : KPPage
    {
         private short RokPlany
        {
          get
            {
                if (ViewState["RokPlany"] == null )
                       ViewState["RokPlany"] = DateTime.Now.Year;
                return short.Parse(ViewState["RokPlany"].ToString());
            }
            set
            {
                ViewState["RokPlany"] = value;

            }

        }
        private void komunikatError(int kod)//, int iT, int iID)
        {
            fError.Visible = true;
            switch (kod)
            {
                case -1: Error1.Visible = true;
                    break;
                case -2: Error2.Visible = true;
                    break;
                case -3: Error3.Visible = true;
                    break;
                case -4: Error4.Visible = true;
                    break;
                case -5: Error5.Visible = true;
                    break;
                case -6: Error6.Visible = true;
                    break;
                default: fError.Visible = false;
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            fError.Visible = false;
            Error1.Visible = false;
            Error2.Visible = false;
            Error3.Visible = false;
            Error4.Visible = false;
            Error5.Visible = false;
            Error6.Visible = false;
            if (!IsPostBack)
            {

                dGrupa.Items.Clear();
                dGrupa.Items.Add(new ListItem() { Text = Stale.wybierzGrupe, Value = "0" });
                foreach (vKP_JednostkiGrupy item in Model.GetGrupyDlaFirmy(int.Parse(Current.FirmaID)))
                {
                    dGrupa.Items.Add(new ListItem()
                    {
                        Text = item.GrupaOpis ,
                        Value = item.GrupaID.ToString()
                    });
                }
              
                lYear.Items.Clear();
                dYear.Items.Clear();
                foreach (short item in Kalendarz.GetYearGdzieWpisanoSwieta())
                {
                    if (RokPlany >= short.Parse(item.ToString()) && short.Parse(item.ToString()) <= DateTime.Now.Year)
                    {
                        this.lYear.Items.Add(item.ToString());
                        this.dYear.Items.Add(item.ToString());
                    }
                }

                dYear.SelectedValue = RokPlany.ToString();
                lYear.SelectedValue = RokPlany.ToString();
   
                tWypelnianieOdData.Text = String.Format("{0:yyyy-MM-dd}",new DateTime(RokPlany, DateTime.Now.Month, DateTime.Now.Day));
                tWypelnianieDoData.Text = "";
                hJednostkaID.Value = Model.GetJednostkaGlownaDlaJednostki(int.Parse(Current.JednostkaID)).ToString();
            }

        }

     protected void DokumentAnkietaOcena_Click(object sender, System.EventArgs e)
        {
            DateTime dd1;
            DateTime dd2;
            if (dGrupa.SelectedValue == "0" || tOpis.Text.Trim()=="")
            {
                komunikatError(-6);
                return;
            }
            if (!DateTime.TryParse(tWypelnianieOdData.Text, out dd1) || !DateTime.TryParse(tWypelnianieDoData.Text, out dd2))
            {
                komunikatError(-5);
                return;
            }
            if (dd1.Date > dd2.Date || dd2.Date < DateTime.Now.Date )
         {
             komunikatError(-5);
             return;
         }
            cJednostki dokument = new cJednostki();
            int outProc = dokument.InsertDokumentAnkietaOcena(
                int.Parse(Current.OsobaID),
                int.Parse(Current.JednostkaID),
                RokPlany,
                int.Parse(dGrupa.SelectedValue),
                this.tOpis.Text,
                dd1,
                dd2,
                12,//case od grupy 12 dla nauczycieli
                null);



            if (outProc < 0)
            {
                komunikatError(outProc);
                return;
            }
            this.ListView1.DataBind();
        }

        protected void urlopRLBRoboczy_Click(object sender, System.EventArgs e)
        {
            //string sTemp = ((KPWeb.Kontrolki.RaportLinkButton)sender).CommandArgument.ToString();
            //for (int i = sTemp.Length; i < Stale.countDigitsInBarCode - 2; i++)
            //{
            //    sTemp = "0" + sTemp;
            //}
            //sTemp = Stale.idWU + sTemp;
            //RaportyPDF.RaportWniosekUrlopowyBarCodePDF(int.Parse(sTemp), 0);
            Server.Transfer("~/AnkietaOcenaPracownika/ListaPracownikowAnkietaOcena.aspx");
        }
        protected void urlopRLBOryginal_Click(object sender, System.EventArgs e)
        {

            //string sTemp = ((KPWeb.Kontrolki.RaportLinkButton)sender).CommandArgument.ToString();

            //for (int i = sTemp.Length; i < Stale.countDigitsInBarCode - 2; i++)
            //{
            //    sTemp = "0" + sTemp;
            //}

            //sTemp = Stale.idWU + sTemp;

            //RaportyPDF.RaportWniosekUrlopowyBarCodePDF(int.Parse(sTemp), 1);
            Server.Transfer("~/AnkietaOcenaPracownika/ListaPracownikowAnkietaOcena.aspx");

        }

       
  protected void btnDokumentKasujClick(object sender, EventArgs e)
        {
            cJednostki dokument = new cJednostki();
            int outProc = dokument.DeleteDokumentAnkietaOcena(
                           int.Parse(((Button)sender).CommandArgument.ToString()));
            if (outProc < 0)
            {
                komunikatError(outProc);
                return;
            }
        
            this.ListView1.DataBind();
        
        }
  protected void btnDokumentZapiszDateWypelnianieDoClick(object sender, EventArgs e)
  {
      DateTime dd1 = DateTime.Parse(((Label)((Button)sender).Parent.FindControl("lWypelnianieOdData")).Text);
      DateTime dd2;

      if (!DateTime.TryParse(((TextBox)((Button)sender).Parent.FindControl("lWypelnianieDoData")).Text, out dd2))
      {
          komunikatError(-5);
          return;
      }
      if (dd1.Date > dd2.Date || dd2.Date < DateTime.Now.Date)
      {
          komunikatError(-5);
          return;
      }
      cJednostki dokument = new cJednostki();
      int outProc = dokument.UpdateDokumentAnkietOcenaWypelnianieDo(
          int.Parse(((Button)sender).CommandArgument.ToString()),dd2,
          int.Parse(Current.OsobaID));
      if (outProc < 0)
      {
          komunikatError(outProc);
          return;
      }

      this.ListView1.DataBind();

  } 
  protected void btnDokumentZaakceptujClick(object sender, EventArgs e)
  {
      cJednostki dokument = new cJednostki();
      int outProc = dokument.UpdateDokumentAnkietOcenaStan(
                     int.Parse(((Button)sender).CommandArgument.ToString()), 0, 1,
          int.Parse(Current.OsobaID));
      if (outProc < 0)
      {
          komunikatError(outProc);
      }
      this.ListView1.DataBind();
 
  }
  protected void btnDokumentCofnijClick(object sender, EventArgs e)
  {
      cJednostki dokument = new cJednostki();
      int outProc = dokument.UpdateDokumentAnkietOcenaStan(
                     int.Parse(((Button)sender).CommandArgument.ToString()), 1, 0,
          int.Parse(Current.OsobaID));
      if (outProc < 0)
      {
          komunikatError(outProc);
      }
      this.ListView1.DataBind();

  }
 

    }
}